/*
 * Copyright (c) 2017. 杭州端点网络科技有限公司.  All rights reserved.
 */

package com.jiantong.user.impl.manager;

import com.jiantong.user.dto.InsertUserGov;
import com.jiantong.user.impl.dao.*;
import com.jiantong.user.model.*;
import com.jiantong.user.service.AppUserRoleWriteService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class UserManagerWeb {

    private final UserDao userDao;
    private final UserProfileDao userProfileDao;
    private final OrganizationUserDao organizationUserDao;
    private final DepartmentUserDao departmentUserDao;
    private final UserRoleDao userRoleDao;
    private final RolesWebDao rolesWebDao;
    private final AppUserRoleWriteService appUserRoleWriteService;


    @Autowired
    public UserManagerWeb(UserDao userDao, UserProfileDao userProfileDao, OrganizationUserDao organizationUserDao, DepartmentUserDao departmentUserDao, UserRoleDao userRoleDao, RolesWebDao rolesWebDao, AppUserRoleWriteService appUserRoleWriteService) {
        this.userDao = userDao;
        this.userProfileDao = userProfileDao;
        this.organizationUserDao = organizationUserDao;
        this.departmentUserDao = departmentUserDao;
        this.userRoleDao = userRoleDao;
        this.rolesWebDao = rolesWebDao;
        this.appUserRoleWriteService = appUserRoleWriteService;
    }

    /**
     * 创建政府用户
     *
     * @param user
     * @param userProfile
     * @param organizationUser
     * @param departmentUser
     * @return
     */
    @Transactional
    public Boolean createOrganizationUser(User user, UserProfile userProfile, OrganizationUser organizationUser, DepartmentUser departmentUser, AppUserRole appUserRole, UserRole userRole) {
        userDao.create(user);
        userProfile.setUserId(user.getId());
        userProfileDao.create(userProfile);
        organizationUser.setUserId(user.getId());
        organizationUser.setUserName(user.getName());
        organizationUserDao.create(organizationUser);
        departmentUser.setUserId(user.getId());
        departmentUser.setUserName(user.getName());
        departmentUserDao.create(departmentUser);
        userRole.setUserId(user.getId());
        Map criteria = new HashMap();
        criteria.put("name", organizationUser.getRoleName());
        RolesWeb rolesWeb = rolesWebDao.findbykeyword(criteria).get(0);
        userRole.setRoleId(rolesWeb.getId());
        userRoleDao.create(userRole);
        appUserRole.setUserId(user.getId());
        appUserRoleWriteService.createAppUserRole(appUserRole);
        return user.getId() != null;
    }

    /**
     * 创建政府用户（list）
     *
     * @param insertUserGovList
     * @return
     */
    @Transactional
    public Boolean createOrganizationUserList(List<InsertUserGov> insertUserGovList) {
        try {
            for (InsertUserGov insertUserGov : insertUserGovList) {
                User user = insertUserGov.getUser();
                UserProfile userProfile = insertUserGov.getUserProfile();
                DepartmentUser departmentUser = insertUserGov.getDepartmentUser();
                AppUserRole appUserRole = insertUserGov.getAppUserRole();
                OrganizationUser organizationUser = insertUserGov.getOrganizationUser();
                userDao.create(user);
                userProfile.setUserId(user.getId());
                userProfileDao.create(userProfile);
                organizationUser.setUserId(user.getId());
                organizationUser.setUserName(user.getName());
                organizationUserDao.create(organizationUser);
                departmentUser.setUserId(user.getId());
                departmentUser.setUserName(user.getName());
                departmentUserDao.create(departmentUser);
                appUserRole.setUserId(user.getId());
                appUserRoleWriteService.createAppUserRole(appUserRole);
            }
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    /**
     * 创建养殖场用户
     *
     * @param user
     * @param userProfile
     * @param organizationUser
     * @return
     */
    @Transactional
    public Boolean createBreedFactoryUser(User user, UserProfile userProfile, OrganizationUser organizationUser) {
        userDao.create(user);
        userProfileDao.create(userProfile);
        userProfile.setUserId(user.getId());
        organizationUser.setUserId(user.getId());
        organizationUser.setUserName(user.getName());
        organizationUserDao.create(organizationUser);
        UserRole userRole = new UserRole();
        userRole.setUserId(user.getId());
        Map criteria = new HashMap();
        criteria.put("name", organizationUser.getRoleName());
        RolesWeb rolesWeb = rolesWebDao.findbykeyword(criteria).get(0);
        userRole.setRoleId(rolesWeb.getId());
        userRoleDao.create(userRole);
        return user.getId() != null;
    }

    @Transactional
    public Boolean updateUser(User user, UserProfile userProfile, OrganizationUser organizationUser, DepartmentUser departmentUser) {
        Boolean u = userDao.update(user);
        Boolean p = userProfileDao.update(userProfile);
        organizationUser.setUserName(user.getName());
        organizationUser.setUserId(user.getId());
        Boolean o = organizationUserDao.update(organizationUser);
        Map criteria = new HashMap();
        criteria.put("userId", user.getId());
        List<UserRole> userRoles = (List<UserRole>) userRoleDao.findbykeword(criteria);
        UserRole userRole = new UserRole();
        if (userRoles != null && !userRoles.isEmpty()) {
            userRole = userRoles.get(0);
        }
        criteria.clear();
        criteria.put("name", organizationUser.getRoleName());
        List<RolesWeb> rolesWebs = rolesWebDao.findbykeyword(criteria);
        RolesWeb rolesWeb = new RolesWeb();
        if (rolesWebs != null && !rolesWebs.isEmpty()) {
            rolesWeb = rolesWebs.get(0);
        }
        userRole.setRoleId(rolesWeb.getId());
        Boolean d = false;
        if (organizationUser.getOrganizationType() == 3) {
            departmentUser.setUserId(user.getId());
            departmentUser.setUserName(user.getName());
            d = departmentUserDao.update(departmentUser);
        } else {
            d = true;
        }
        return u && p && o && d;
    }

}
